import { Component, OnInit, ViewChild } from '@angular/core';
import { AjaxService } from "../../commons/ajax.service";

@Component({
  selector: 'app-org',
  templateUrl: './org.component.html',
  styleUrls: ['./org.component.css']
})
export class OrgComponent implements OnInit {

  private tree_data_2 = [];

  constructor(
    private ajaxSerivce: AjaxService
  ) { }

  ngOnInit() {

    
    this.getChildrenOrgList();
  }

  getChildrenOrgList() {

    $('#tree2').ace_tree({
      dataSource: (options, callback) => {
        let parentId, orgArray;
        if(options.data == null) {
          parentId = 0;
          orgArray = this.tree_data_2;
        }else {
          parentId = options.data.id;
          if(!options.children) options.children = [];
          orgArray = options.children;
        }
        this.ajaxSerivce.post('/api/sys/org/children', {parentId: parentId}).then((response) => {
          let array = response.json();
          for (let obj of array) {
            orgArray.push({
              text: obj.orgName,
              type: 'folder',
              data: obj
            });
          }
          callback({ data: orgArray });
        });
      },
      loadingHTML:'<div class="tree-loading"><i class="ace-icon fa fa-refresh fa-spin blue"></i></div>',
      'open-icon' : 'ace-icon fa fa-folder-open',
      'close-icon' : 'ace-icon fa fa-folder',
      'itemSelect' : true,
      'folderSelect': true,
      'multiSelect': true,
      'selected-icon' : null,
      'unselected-icon' : null,
      'folder-open-icon' : 'ace-icon tree-plus',
      'folder-close-icon' : 'ace-icon tree-minus'
    });
  }
}
